import Vue from "vue";
import SuiVue from "semantic-ui-vue";
import VeeValidate from "vee-validate";
import mavonEditor from "mavon-editor";
import VueAxios from "vue-axios";
import axios from "axios";
import router from "./router";
import VueAuth from "@websanova/vue-auth";
import store from "./store";
import { save } from "service/api";
import "mavon-editor/dist/css/index.css";
import "semantic-ui-css/semantic.min.css";
Vue.router = router;
Vue.use(mavonEditor);
Vue.use(SuiVue);
Vue.use(VeeValidate);
Vue.use(VueAxios, axios);
Vue.use(VueAuth, {
  auth: {
    request: function(req, token) {
      console.log(token);
      this.options.http._setHeaders.call(this, req, {
        Authorization: `Bearer ${window.localStorage.getItem("auth-token")}`
      });
    },
    response: function(res) {
      // 如果
      if (!store.getters.app.isLoggedIn) {
        save("http://192.168.0.114:8080/auth/user", {
          token: window.localStorage.getItem("auth-token")
        })
          .then(response => {
            if (response.status == "200") {
              store.dispatch("loggedIn");
              let result = res.data;
              store.dispatch("setUser", result);
            }
          })
          .catch(error => {
            store.dispatch("loggingIn");
            console.log(error);
          });
      }
      return res;
    }
  },
  http: require("@websanova/vue-auth/drivers/http/axios.1.x"),
  router: require("@websanova/vue-auth/drivers/router/vue-router.2.x"),
  loginData: {
    url: "http://192.168.0.114:8080/auth/login",
    fetchUser: false
  },
  refreshData: {
    enabled: false
  }
});
export default ({
  Vue, // VuePress 正在使用的 Vue 构造函数
  options, // 附加到根实例的一些选项
  router, // 当前应用的路由实例
  siteData, // 站点元数据,
  store
}) => {};
